<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .a{
            width: 50px;
            height: 50px;
            background-color: #f00;
            position: absolute;
            left: 1200px;
        }
    </style>
</head>
<body>

    <div class="a"></div>

    <script>

        var oDiv = document.querySelector('div') ;
        move(oDiv , 'left' , 1 , 0) ;

        function move(ele, prop, speed, end, cb) {
            flag = false ;
            // 确定速度？  根据终点位置和起点位置的值
            var x = getCss(ele, prop);   // 样式获取到的是字符串
            x = parseInt(x);
            speed = end > x ? speed : -speed;
            var t = setInterval(function () {
                // speed++ ;
                speed > 0 ? speed++ : speed-- ;
                x += speed;
                ele.style[prop] = x + 'px';
                if (Math.abs(end - x) <= Math.abs(speed)) {
                    clearInterval(t);
                    // 到达终点
                    ele.style[prop] = end + 'px';
                    flag = true ;
                    cb && cb();

                }
            }, 20)
        }

        
        // 获取样式
        function getCss(ele, prop) {
            if (getComputedStyle) {
                return getComputedStyle(ele)[prop]
            }
            return ele.currentStyle[prop]
        }

    </script>
    
</body>
</html>